package net.mobgame.android.control;

import java.util.ArrayList;
import net.mobgame.android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MenuAdapter extends BaseAdapter {
	private final int VIEWTYPE_NORMAL = 0;
	private ArrayList<MenuItem> items;
	private Context context;

	public MenuAdapter(Context context) {
		this.context = context;
		loadItems();
	}

	private void loadItems() {
		String[] tmp = context.getResources().getStringArray(R.array.menuItems);
		String[] tempAdress = context.getResources().getStringArray(R.array.menuAdress);
		items = new ArrayList<MenuItem>();
		for (int i = 0; i < tmp.length; i++) {
			String[] s = tmp[i].split("_");
			int id = Integer.parseInt(s[0]);
			int sub_id = Integer.parseInt(s[1]);
			items.add(new MenuItem(id, s[2], tempAdress[i], sub_id)); //danh sach luc dang nhap
		}
	}

	@Override
	public int getCount() {
		return items.size();
	}
	@Override
	public MenuItem getItem(int position) {
		return items.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(int pos, View convert, ViewGroup parent) {
		if (convert == null) {
			LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if (getItemViewType(pos) == VIEWTYPE_NORMAL) {
				convert = inf.inflate(R.layout.action_list_item, parent, false);
			} 
		}
		TextView txt_label = (TextView) convert.findViewById(R.id.txt_item);
		txt_label.setText(items.get(pos).label);
		int resource = convert.getResources().getColor(R.color.white);
		txt_label.setTextColor(resource);
		return convert;
	}

	public void update() {
		notifyDataSetChanged();
	}

	public class MenuItem {
		public int id;
		public int sub_id;
		public String address;
		public String label;

		public MenuItem(int pid, String plabel, String paddress,	int pimg_id) {
			id = pid;
			label = plabel;
			address = paddress;
			sub_id = pimg_id;
		}
	}
}
